home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12291 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: news.spies.com!usenet
  2. From: Erik Max Francis <max@alcyone.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Is This Bad Coding Practice?
  5. Date: Sat, 30 Mar 1996 00:57:46 -0800
  6. Organization: Alcyone Systems
  7. Message-ID: <315CF78A.4F23D93C@alcyone.com>
  8. References: <4jgnt2$9d1@loki.tor.hookup.net>
  9. NNTP-Posting-Host: newton.alcyone.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.01 (X11; I; Linux 1.2.13 i486)
  14.  
  15. Rajendra Singh wrote:
  16.  
  17. > ... since I am using the value returned from func1() immediately (in
  18. > main()), is this reliable?  After I copy it into "string", I won't be
  19. > using that area of memory anymore (i. e. the pointer returned by
  20. > func1()).
  21.  
  22. No, it is not reliable.  Once control leaves a block, the local variables in
  23. that block go out of scope.  Your string in the function goes out of scope when
  24. you return from the function, and so you can't assume that pointers to that
  25. local variable will still point to valid data outside of its scope.
  26.  
  27. This is one of the worst kinds of bugs; oftentimes, in most compilers, the
  28. operation will _almost_ always work.  Occasionally, though, it will fail.
  29.  
  30. -- 
  31. Erik Max Francis &tSftDotIotE && http://www.alcyone.com/max && max@alcyone.com
  32. San Jose, California, U.S.A. && 37 20 07 N 121 53 38 W && the 4th R is respect
  33. H.3`S,3,P,3$S,#$Q,C`Q,3,P,3$S,#$Q,3`Q,3,P,C$Q,#(Q.#`-"C`- && 1love && folasade
  34. Omnia quia sunt, lumina sunt. && Dominion, GIGO, GOOGOL, Omega, Psi, Strategem
  35. "Out from his breast/his soul went to seek/the doom of the just." -- _Beowulf_
  36.